
<?php 

class MySqlDB{
	private $_host;
	private $_user;
	private $_pass;
	private $_char;
	private $_db;
	private $_link;
    private static $instance;
    
	private function __construct($config=''){
		$this->_init($config);
		$this->_connect();
		$this->_char();
		$this->_db();
	}
    private function __clone(){}
    public static function getinstance(){
        if (!isset(static::$instance)) {
            static::$instance=new MysqlDB();
        }return static::$instance;
    } 

	private function _init($config){
		$this->_host=isset($config['host'])?$config['host']:'localhost';
		$this->_user=isset($config['user'])?$config['user']:'root';
		$this->_pass=isset($config['pass'])?$config['pass']:'root';
		$this->_char=isset($config['char'])?$config['char']:'utf8';
		$this->_db=isset($config['db'])?$config['db']:'liuyingban';
	}
	
	private function _connect(){
		$link = @mysql_connect($this->_host,$this->_user,$this->_pass);
		if($link){
			$this->_link = $link;
		}else{
			die('连接数据库失败,错误原因：'.mysql_error());
		}
	}
    private function _char(){
    	mysql_set_charset($this->_char);
    }
    private function _db(){
    	mysql_select_db($this->_db);
    }
    public function query($sql){
    	$res = mysql_query($sql);
    	if ($res) {
    		$return=array();
    		while ($row=mysql_fetch_assoc($res)) {
    			$return[]=$row;
    		}
    		mysql_free_result($res);
    		return $return;
    	}else{
    		die('查询语句有错语句是'.$sql.'错误原因:'.mysql_error());
    	}
    }
    public function exec($sql){
    	$res = mysql_query($sql);
    	if ($res) {
    	    return mysql_affected_rows();
    	}else{
    		die('增删改语句有错语句是'.$sql.'错误原因:'.mysql_error());
    	}

    }
    public function __destruct(){
    	mysql_close($this->_link);
    }
}
 ?>
